#include "rfid.h"
#include <QDebug>
#include <QMutex>
#include<termios.h>
#include<unistd.h>
#define NOTCARDIDTIME 150
RFID::RFID(int fd)
{
    this->fd = fd;
    CardId="";
    test=0;
    IsFind=false;
    m_nottimes=0;
}

bool RFID::GetCardId()
{
    debugOut("**********Start Read Label****************");
    unsigned char cmd[8];
    cmd[0]=0xA0;
    cmd[1]=0x06;
    cmd[2]=0x80;
    cmd[3]=0x00;
    cmd[4]=0x01;
    cmd[5]=0x02;
    cmd[6]=0x01;
    cmd[7]=0xD6;
    tcflush(fd,TCIOFLUSH);
    int len=::write(fd,cmd,sizeof(cmd));
    if(len==8)
        return true;
    else
        return false;

}

QString RFID::hexToString(unsigned char *in, int len)
{
    int i;
    unsigned char inChar,hi,lo;
    QString s;

    for (i=0; i<len; i++)
    {
        inChar = in[i];

        hi = (inChar & 0xF0) >> 4;
        if (hi > 9)
            hi = 'A' + (hi - 0x0A);
        else
            hi += 0x30;
        s.append(hi);

        lo = inChar & 0x0F;
        if (lo > 9)
            lo = 'A' + (lo - 0x0A);
        else
            lo += 0x30;
        s.append(lo);
    }

    return s;
}

bool RFID::AnalyzeCarInfo(QString rfid, CardInfo &car_info, QHash<QString, QString>& hash)
{
    if(rfid.count()!=24)
        return false;
    QString plate;
    QString proNum;
    QString city;
    QString num;
    QString tare;
    QString type;
    proNum=rfid.mid(2,2);
    city=rfid.mid(4,2);
    num=rfid.mid(6,12);
    type=rfid.mid(18,2);
    tare=rfid.mid(20,2);
    if(hash.isEmpty())
        qDebug()<<"HashPlate is Empty";
    QHashIterator<QString,QString> i(hash);
    while(i.hasNext())
    {
        i.next();
        if(i.value()==proNum)
            plate+=i.key();
    }
    city=QChar(city.toInt(NULL,16));
    plate+=city;
    QString num_1=num.left(2);
    if(num_1!="00")
        plate+="D";
    for(int i=0;i<5;i++)
    {
        QString num_1=num.mid(i*2+2,2);
        if(num_1>="00" && num_1<="09")
            num_1=num_1.right(1);
        else
            num_1=num_1.toInt(NULL,16);
        plate+=num_1;
    }
    car_info.plate=plate;
    car_info.carType=type.toInt(NULL,16);
    car_info.carTare=tare.toInt(NULL,16);
    if(car_info.carTare>21)
        car_info.carTare/=10;
    qDebug()<<"plate="<<car_info.plate<<"type="<<car_info.carType<<"tare="<<car_info.carTare;
    return true;
}


void RFID::runThread()
{
    unsigned char bytes[100];
    ::memset(bytes,'\0',sizeof(bytes));
    short idx = 0;
    char data[2];
    unsigned char c ;
    int flag_start = 0;
    m_nottimes=0;
    while(1) {
        //qDebug() << "RFID Thread" << endl;
        int n=::read(fd,data,1);
        c=data[0];
        if(m_nottimes>=NOTCARDIDTIME)
        {
            if(!CardId.isEmpty())
                CardId.clear();
            if(Get_CardId != CardId) {
                Get_CardId = CardId;
                emit signalToMainrfid(CardId);
            }
            m_nottimes=0;
        }
        if(n <= 0) {
            m_nottimes++;
            usleep(30000);
            continue;
        }
        m_nottimes=0;
        if(c == 0xED && flag_start==0/*|| idx > 11)*/ ){
            idx = 0;
            flag_start =1;
        }
        if(flag_start == 1 )
        {
            bytes[idx] = c;
            idx++;
            if(idx>12)
            {
                flag_start=0;
                idx=0;
            }
            if(c == 0xDE && idx==12)
            {
                idx = 0;
                flag_start = 0;
                CardId=hexToString(bytes,12);
                if(CardId.indexOf("ED")!=-1 && CardId.indexOf("DE")!=-1) {
                    if(Get_CardId != CardId) {
                        Get_CardId = CardId;
                        emit signalToMainrfid(CardId);
                    }
                }

                m_nottimes=0;
                //qDebug() << "RFID Thread End" << endl;
                return;
            }

        }
    }

    usleep(10000);//200
}

bool RFID::FindEPCCard()
{
    debugOut("***********Start Find EPC******************");
    unsigned char cmd[5];
    ::memset(cmd,'\0',5);
    cmd[0]=0xA0;
    cmd[1]=0x03;
    cmd[2]=0x82;
    cmd[3]=0x00;
    cmd[4]=0xDB;
    tcflush(fd,TCIOFLUSH);
    int len=::write(fd,cmd,5);
    if(len==5)
        return true;
    else
        return false;

}

void RFID::run()
{
    debugOut("Start RFID Thread!!!!");
    //QMutex mutex;
    while(1)
    {
        runThread();
    }
}
